<template>
    <van-nav-bar title="编辑收货地址" left-text="" left-arrow @click-left="goBack" />
    <van-address-edit :area-list="areaList" show-set-default :area-columns-placeholder="['请选择', '请选择', '请选择']"
        @save="onSave" show-delete :address-info="info" @delete="onDelete" />

</template>
<script setup>
import { areaList } from '@vant/area-data';
const { proxy } = getCurrentInstance()
const goBack = () => { proxy.$router.go(-1) }
const LoginUser = reactive(proxy.$cookies.get('LoginUser') ? proxy.$cookies.get('LoginUser') : {})
const addressid = proxy.$route.query.addressid ? proxy.$route.query.addressid : 0;
const info = ref({})
//挂载前获取回显数据
onBeforeMount(() => {
    getAddressInfoData()
})
const getAddressInfoData = async () => {
    let result = await Api.addressInfo({ busid: LoginUser.id, addressid: addressid })
    if (result.code == 1) {
        let data = result.data
        info.value = {
            id: data.id,
            name: data.consignee,
            tel: data.mobile,
            isDefault: data.status == '1' ? true : false,
            areaCode: data.district,
            addressDetail: data.address
        }
    }
}
const onSave = async (val) => {
    let data = {
        addressid: addressid,
        busid: LoginUser.id,
        consignee: val.name,
        address: val.addressDetail,
        mobile: val.tel,
        status: val.isDefault == true ? '1' : '0',
        region: val.areaCode
    }

    let result = await Api.addressEdit(data)
    if (result.code == 1) {
        success({
            message: result.msg,
            onClose: () => {
                goBack()
            }
        });

    } else {
        error(result.msg);
    }
}

const onDelete = (val) => {
    showConfirmDialog({
        title: '删除',
        message:
            '确认是否删除收货地址?',
    })
        .then(async () => {
            let result = await Api.addressDel({ busid: LoginUser.id, addressid: val.id })
            if (result.code == 1) {
                goBack()
            }
            else {
                error(result.msg);
            }
        })
        .catch(() => { });
}
</script>